package com.suixin.learn.persistence;

import com.suixin.learn.persistence.factory.SqlSession;
import com.suixin.learn.persistence.factory.SqlSessionFactory;
import com.suixin.learn.persistence.factory.SqlSessionFactoryBuilder;
import com.suixin.learn.persistence.io.Resources;
import com.suixin.learn.persistence.pojo.User;

import java.util.List;

public class PersistenceTest {

    public static void main(String[] args) throws Exception {
        SqlSessionFactory build = SqlSessionFactoryBuilder.build(Resources.getResource("sqlMapConfig.xml"));
        SqlSession sqlSession = build.openSession();

        IUserDao mapper = sqlSession.getMapper(IUserDao.class);
        List<User> userList = mapper.findAll();

//        List<User> userList = sqlSession.<User>selectList("com.suixin.learn.persistence.pojo.User.selectList", "selectList");
        System.out.println(userList);

        User user = new User();
        user.setId(1);
        user.setUsername("suixin");
//        User resultUser = sqlSession.<User>selectOne("com.suixin.learn.persistence.pojo.User.selectOne",user);
        User resultUser = mapper.findOne(user);
        System.out.println(resultUser);

        User upuser = new User();
        upuser.setId(9);
        upuser.setUsername("suixinuyyyjjj");
//        int update = sqlSession.update("com.suixin.learn.persistence.IUserDao.update", upuser);
        int update = mapper.update(upuser);
        System.out.println("update count :"+update);


        User insertUser = new User();
        insertUser.setUsername("insertUser");
//        int insert = sqlSession.insert("com.suixin.learn.persistence.IUserDao.insert", insertUser);
        int insert = mapper.insert(insertUser);
        System.out.println("insert count :"+insert);


        User deleteUser = new User();
        deleteUser.setId(6);
//        int delete = sqlSession.delete("com.suixin.learn.persistence.IUserDao.delete", deleteUser);
        int delete = mapper.delete(deleteUser);
        System.out.println("deleteUser count :"+delete);
    }
}
